import { DatePicker, Switch, Checkbox, Slider, Input, InputNumber, Select, Rate } from "antd";
import { VText, VImage } from "voyage-component";
import VRadio from "components/CMSComponents/VRadio";
import VButton from "components/CMSComponents/VButton";
import { CMSCompWrapperHOC } from "views/CMSEditor/components/CMSCompWrapper";
import { wrapperComp } from "views/CMSEditor/config";
import VNumber from "components/CMSComponents/VNumber";
import VTag from "components/CMSComponents/VTag";
import VTagGroup from "components/CMSComponents/VTagGroup";
import VTime from "components/CMSComponents/VTime";
import VButtonGroup from "components/CMSComponents/VButtonGroup";
import VReactEcharts from "components/CMSComponents/VReactEcharts";
const { Password } = Input;
const CheckboxGroup = Checkbox.Group;
const CMSCompMap: {[key: string]: any} = {
    DatePicker: DatePicker,
    Switch: Switch,
    Checkbox: Checkbox,
    Slider: Slider,
    Input: Input,
    Password: Password,
    InputNumber: InputNumber,
    Select: Select,
    VRadio: VRadio,
    CheckboxGroup: CheckboxGroup,
    Rate: Rate,
    VButton: VButton,
    VText: VText,
    VNumber: VNumber,
    VTag: VTag,
    VImage: VImage,
    VTagGroup: VTagGroup,
    VTime: VTime,
    VButtonGroup: VButtonGroup,
    VReactEcharts: VReactEcharts
}

const transformChildrenToFormcolumns = (children: any[], preview?: boolean) => {
    return {
        columns: children.map(item => {
            return {
                component: CMSCompMap[item.component],
                componentStr: item.component,
                wrapperComp: preview ? undefined : CMSCompWrapperHOC({id: item.id, isWrapper: wrapperComp.includes(item.component)}),
                formExtra: item.props?.formExtra || {},
                extra: item.props,
                label: item.props?.label,
                field: item.props?.field
            }
        })
    }
}

const transformChildrenToTableColumns = (children: any[], preview?: boolean, node?: any) => {
    return {
        columns: children.map(item => {
            return {
                ...item?.columnProps,
                render: (text: any, record: any) => {
                    const Component = CMSCompMap[item.component];
                    return <Component {...item.props} {...{[item.props?._injectValueKey || "text"]: text}} />
                }
            }
        })
    }
}

const transformChildrenToEchartsColumns = (children: any[], preview?: boolean, node?: any) => {
    return {
        echartsNode: children.map(item => {
            return {
                component: () => {
                    const Charts = CMSCompMap[item.component];
                    return <Charts {...item.props} />
                },
                wrapperComp: preview ? undefined : CMSCompWrapperHOC({id: item.id, isWrapper: wrapperComp.includes(item.component)}),
            };
        }),
        
    }
}
export const transformChildrenToProps = (children: any[], wrapper: string, preview?: boolean, node?: any) => {
    switch(wrapper){
        case "VFormWrapper":
            return transformChildrenToFormcolumns(children, preview);
        case "VTableWrapper":
            return transformChildrenToTableColumns(children, preview, node);
        case "VEchartsWrapper":
            return transformChildrenToEchartsColumns(children, preview, node)
        default:
            return []
    }
}

export const isObject = (value: any) => {
    return Object.prototype.toString.call(value) === "[object Object]";   
}

export const combineObject = (obj1: {[key: string]: any}, obj2: {[key: string]: any}) => {
    const newObj = Object.create(obj1 || {});
    Object.keys(obj2).forEach(key => {
        if(!Reflect.has(newObj, key)){
            Reflect.set(newObj, key, obj2[key]);
            return
        }
        const oneValue = Reflect.get(newObj, key);
        const secondValue = Reflect.get(obj2, key);
        let newValue = undefined;
        if(Array.isArray(oneValue)){
            newValue = oneValue.concat(Array.isArray(secondValue) ? secondValue : [secondValue]);
        } else if(isObject(oneValue)){
            newValue = {
                ...oneValue,
                ...secondValue
            }
        } else {
            newValue = secondValue;
        }
        Reflect.set(newObj, key, newValue);
    });
    return newObj;
}